import java.util.Scanner;

//6 5
//1 1 1 0 0
//1 1 1 0 0
//1 1 1 0 0
//0 0 0 1 1
//0 0 1 0 1
//1 1 0 1 0
//5
//
//2 2
//1 0
//0 1
//2

public class 在01矩阵中找到1块 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int[][] a = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
            System.out.println(count(a));
        }
        sc.close();
    }

    private static int count(int[][] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                count += (mark(arr, i, j) >= 1) ? 1 : 0;
            }
        }
        return count;
    }

    private static int mark(int[][] arr, int i, int j) {
        int count = 0;
        if (i >= 0 && j >= 0 && i < arr.length && j < arr[i].length && arr[i][j] == 1) {
            count++;
            arr[i][j] = -1;
            count += mark(arr, i + 1, j);
            count += mark(arr, i - 1, j);
            count += mark(arr, i, j + 1);
            count += mark(arr, i, j - 1);
        }
        return count;
    }
}
